package com.gaoqi.mq.work;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class WorkConsumer {

    //rabbitmq work 工作模式不需要交换机
//    @RabbitListener(queuesToDeclare = @Queue("work"))
//    public void receiveMessageFromWork(Channel channel, Message message){
//            log.info("消费线程消费:"+new String(message.getBody()));
//    }

    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1MessageOfWork(Channel channel,Message message){
        //手动确认
        try {
            Thread.sleep(1000);
            log.info("消费者1:消费"+message);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("逻辑处理！！！");
        }
    }


    @RabbitListener(queuesToDeclare = @Queue("nWork"))
    public void receive2MessageOfWork(Channel channel,Message message){
        //手动确认
        try {
            Thread.sleep(1000);
            log.info("消费者2:消费"+message);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("逻辑处理！！！");
        }
    }
}
